import uuid

from flask import Blueprint, request, jsonify, send_file
from service.nw_szy.nw_szy_mx_service import get_all_data, get_data_by_uuid, add_data, update_data, delete_data, calc_szy, calc_szy_mx,download_excel,check_szy_mx_ready,check_row_define_ready,check_column_define_ready

# app = Flask(__name__)
# 创建 Blueprint
nw_szy_mx_blueprint = Blueprint('nw_szy_mx', __name__)


# 获取所有记录
@nw_szy_mx_blueprint.route('/api/data', methods=['GET'])
def get_data():
    try:
        data = get_all_data()
        return jsonify(data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 获取单条记录
@nw_szy_mx_blueprint.route('/api/data/<string:uuid>', methods=['GET'])
def get_single_data(uuid):
    try:
        data = get_data_by_uuid(uuid)
        if data:
            return jsonify(data), 200
        else:
            return jsonify({"message": "Data not found"}), 404
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 添加记录
@nw_szy_mx_blueprint.route('/api/data', methods=['POST'])
def add_new_data():
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        model = data.get('model')
        xhbh = data.get('xhbh')
        xhmc = data.get('xhmc')
        fybh = data.get('fybh')
        fymc = data.get('fymc')
        kjnd = data.get('kjnd')
        kjqj = data.get('kjqj')
        pzrq = data.get('pzrq')
        pzbh = data.get('pzbh')
        zdr = data.get('zdr')
        zy = data.get('zy')
        kmbh = data.get('kmbh')
        kmmc = data.get('kmmc')
        jzfx = data.get('jzfx')
        je = data.get('je')
        bmbh = data.get('bmbh')
        bmmc = data.get('bmmc')
        wldwbh = data.get('wldwbh')
        wldwmc = data.get('wldwmc')
        zx01bh = data.get('zx01bh')
        zx01mc = data.get('zx01mc')
        zx01qc = data.get('zx01qc')
        zx02bh = data.get('zx02bh')
        zx02mc = data.get('zx02mc')
        zx02qc = data.get('zx02qc')
        zx99bh = data.get('zx99bh')
        zx99mc = data.get('zx99mc')
        zx99qc = data.get('zx99qc')

        add_data(uuid.uuid4(), dwbh, model, xhbh, xhmc, fybh, fymc, kjnd, kjqj, pzrq, pzbh, zdr, zy, kmbh, kmmc, jzfx, je, bmbh, bmmc, wldwbh, wldwmc,
                 zx01bh, zx01mc, zx01qc, zx02bh, zx02mc, zx02qc, zx99bh, zx99mc, zx99qc)
        return jsonify({"message": "Data added successfully!"}), 201
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 更新记录
@nw_szy_mx_blueprint.route('/api/data/<string:uuid>', methods=['PUT'])
def update_existing_data(uuid):
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        model = data.get('model')
        xhbh = data.get('xhbh')
        xhmc = data.get('xhmc')
        fybh = data.get('fybh')
        fymc = data.get('fymc')
        kjnd = data.get('kjnd')
        kjqj = data.get('kjqj')
        pzrq = data.get('pzrq')
        pzbh = data.get('pzbh')
        zdr = data.get('zdr')
        zy = data.get('zy')
        kmbh = data.get('kmbh')
        kmmc = data.get('kmmc')
        jzfx = data.get('jzfx')
        je = data.get('je')
        bmbh = data.get('bmbh')
        bmmc = data.get('bmmc')
        wldwbh = data.get('wldwbh')
        wldwmc = data.get('wldwmc')
        zx01bh = data.get('zx01bh')
        zx01mc = data.get('zx01mc')
        zx01qc = data.get('zx01qc')
        zx02bh = data.get('zx02bh')
        zx02mc = data.get('zx02mc')
        zx02qc = data.get('zx02qc')
        zx99bh = data.get('zx99bh')
        zx99mc = data.get('zx99mc')
        zx99qc = data.get('zx99qc')

        update_data( uuid, dwbh, model, xhbh, xhmc, fybh, fymc, kjnd, kjqj, pzrq, pzbh, zdr, zy, kmbh, kmmc, jzfx, je, bmbh, bmmc, wldwbh, wldwmc,
                 zx01bh, zx01mc, zx01qc, zx02bh, zx02mc, zx02qc, zx99bh, zx99mc, zx99qc)
        return jsonify({"message": "Data updated successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 删除记录
@nw_szy_mx_blueprint.route('/api/data/<string:uuid>', methods=['DELETE'])
def delete_existing_data(uuid):
    try:
        delete_data(uuid)
        return jsonify({"message": "Data deleted successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@nw_szy_mx_blueprint.route('/api/calc_szy', methods=['POST'])
def calc_szy_controller():
    """
    生成收支余结果表，数据保存到数据库表
    :param p_dwbh: 单位编号
    :param p_ledger: 账套ID
    :param p_model: 模型
    :param p_qzq_type: 收支余类型, 可不传
    :param p_kjnd: 会计年度，可不传
    """

    try:
        data = request.get_json()
        p_dwbh = data.get('dwbh')
        p_ledger = data.get('ledger')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        p_kjnd = data.get('kjnd', None)

        calc_szy(p_dwbh, p_ledger, p_model, p_qzq_type, p_kjnd)
        return jsonify({"code":200, "message": "计算完成"}), 200
    except Exception as e:
        return jsonify({"code": 500, "error": str(e)}), 500


@nw_szy_mx_blueprint.route('/api/calc_szy_mx', methods=['POST'])
def calc_szy_mx_controller():
    """
    生成收支余明细结果表，数据保存到数据库表
    :param p_dwbh: 单位编号
    :param p_ledger: 账套ID
    :param p_model: 模型
    :param p_qzq_type: 收支余类型, 可不传
    :param p_kjnd: 会计年度，可不传
    """

    try:
        data = request.get_json()
        p_dwbh = data.get('dwbh')
        p_ledger = data.get('ledger')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        p_kjnd = data.get('kjnd', None)

        calc_szy_mx(p_dwbh, p_ledger, p_model, p_qzq_type, p_kjnd)
        return jsonify({"code":200, "message": "计算完成"}), 200
    except Exception as e:
        return jsonify({"code": 500, "error": str(e)}), 500


@nw_szy_mx_blueprint.route('/api/download', methods=['POST'])
def download_data_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        excel_filename, excel_path = download_excel(p_dwbh, p_model, p_qzq_type)
        return send_file(excel_path, as_attachment=True, download_name=excel_filename, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    except Exception as e:
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500


@nw_szy_mx_blueprint.route('/api/check_szy_mx_ready', methods=['POST'])
def check_szy_mx_ready_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        result = check_szy_mx_ready(p_dwbh, p_model, p_qzq_type)
        return jsonify({'code': 200, 'data': result}), 200
    except Exception as e:
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500

@nw_szy_mx_blueprint.route('/api/check_row_define_ready', methods=['POST'])
def check_row_define_ready_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        p_kjnd = data.get('kjnd')
        result = check_row_define_ready(p_dwbh, p_model, p_qzq_type, p_kjnd)
        return jsonify({'code': 200, 'data': result}), 200
    except Exception as e:
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500


@nw_szy_mx_blueprint.route('/api/check_column_define_ready', methods=['POST'])
def check_column_define_ready_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_kjnd = data.get('kjnd')
        result = check_column_define_ready(p_dwbh, p_model, p_kjnd)
        return jsonify({'code': 200, 'data': result}), 200
    except Exception as e:
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
    # 在应用启动时创建表
    from service.nw_szy.nw_szy_mx_service import create_table

    create_table()
    # app.run(debug=True)
